package com.news.servlet;

import com.news.db.OpDB;
import com.news.model.News;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;

/**
 * 添加新闻
 */
@WebServlet("/addnews")
public class AddNewsServlet extends HttpServlet {
    private boolean check(HttpServletRequest request, HttpServletResponse response) throws IOException {
        response.setContentType("text/html;charset=utf-8");
        PrintWriter out = response.getWriter();
        String info_type = request.getParameter("info_type");
        if (info_type.equals("-1")) {
            out.println("<script>alert('新闻类别不能为空'); window.history.go(-1);</script>");
            return false;
        }

        String info_title = request.getParameter("info_title");
        if (info_title == null || info_title.equals("")) {
            out.println("<script>alert('新闻标题不能为空'); window.history.go(-1);</script>");
            return false;
        } else if (info_title.length() > 60) {
            out.println("<script>alert('新闻标题不能超过30个汉字'); window.history.go(-1);</script>");
            return false;
        }

        String info_content = request.getParameter("info_content");
        if (info_content == null || info_content.equals("")) {
            out.println("<script>alert('新闻内容不能为空'); window.history.go(-1);</script>");
            return false;
        } else if (info_content.length() > 6000) {
            out.println("<script>alert('新闻内容不能超过3000个汉字'); window.history.go(-1);</script>");
            return false;
        }

        String info_creator = request.getParameter("info_creator");
        if (info_creator == null || info_creator.equals("")) {
            out.println("<script>alert('新闻作者不能为空'); window.history.go(-1);</script>");
            return false;
        }
        return true;
    }

    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        this.doPost(req, resp);
    }

    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        if (check(request, response)) {
            int info_type = Integer.parseInt(request.getParameter("info_type"));
            String info_title = request.getParameter("info_title");
            String info_content = request.getParameter("info_content");
            String info_creator = request.getParameter("info_creator");
            News n = new News(info_type, info_title, info_content, info_creator);
            PrintWriter out = response.getWriter();
            OpDB opdb = new OpDB();
            if (opdb.OpAddObject(n)) {
                out.println("<script>alert('新闻添加成功！'); window.location.href='admin/IndexTemp.jsp';</script>");
            } else {
                out.println("<script>alert('新闻添加失败！'); window.location.href='admin/IndexTemp.jsp';</script>");
            }

        }
    }
}
